package LeetCode;

import Constructs.ListNode;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/5/2.
//
//https://leetcode.cn/problems/swap-nodes-in-pairs/
public class 两两交换链表节点 {
    public static void main(String[] args) {
        ListNode list = new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,null))));
        System.out.println(swapPairs(list));
    }
    public static ListNode swapPairs(ListNode head) {
        if(head == null)return null;
        if(head.next == null)return head;
        ListNode point = new ListNode(0,head);
        ListNode pre = point;
        ListNode mid = head;
        ListNode next = head.next;
        while(next != null){
            mid.next = next.next;
            next.next = mid;
            pre.next = next;
            if(mid.next == null)break;
            pre = mid;
            mid = mid.next;
            next = mid.next;
        }
        return point.next;
    }
}
